package com.ryl.yomall.weball.controller;

import com.ryl.yomall.model.common.result.Result;
import com.ryl.yomall.model.product.BaseCategoryView;
import com.ryl.yomall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Date 17:28
 * @Author ryl
 */
@Controller
public class IndexController {


    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private TemplateEngine templateEngine;
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() throws FileNotFoundException, UnsupportedEncodingException {
        Context context=new Context (  );
        context.setVariable ( "list",getData () );
        PrintWriter printWriter = new PrintWriter ( "D:\\temp\\index.html" ,"utf-8" );
        templateEngine.process ( "index/index",context,printWriter );
        System.out.println ("页面生成!" );
        return Result.ok ();
    }
    public List<Map<String,Object>> getData(){
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList ( );
        List<Map<String,Object>> resultListMap= new ArrayList<> (  );
        Map<Long, List<BaseCategoryView>> category1IdListMap = baseCategoryViewList.stream ( )
                .collect ( Collectors.groupingBy (BaseCategoryView::getCategory1Id ) );
        int index=1;
        for (Map.Entry<Long, List<BaseCategoryView>> category1IdEntry : category1IdListMap.entrySet ( )) {
            Map<String,Object> category1IdMap =new HashMap<> (  );
            List<BaseCategoryView> category1IdValue = category1IdEntry.getValue ( );
            category1IdMap.put ( "index",index++ );
            category1IdMap.put ( "categoryId",category1IdEntry.getKey () );
            category1IdMap.put ( "categoryName",category1IdValue.get ( 0 ).getCategory1Name () );
            // 子分类集合,二级分类数据
            Map<Long, List<BaseCategoryView>> category2IdListMap = category1IdValue.stream ( ).collect ( Collectors.groupingBy ( BaseCategoryView::getCategory2Id ) );
            List<Map<String,Object>> category2IdMapList= new ArrayList<> (  );
            for (Map.Entry<Long, List<BaseCategoryView>> category2IdEntry : category2IdListMap.entrySet ( )) {
                Map<String,Object> category2IdMap =new HashMap<> (  );
                List<BaseCategoryView> category2IdValue = category2IdEntry.getValue ( );
                category2IdMap.put ( "categoryId",category2IdEntry.getKey ());
                category2IdMap.put ( "categoryName",category2IdValue.get ( 0 ).getCategory2Name ());
                //子集合 三级分类数据
                List<Map> mapList = category2IdValue.stream ( ).map ( baseCategoryView -> {
                    Map map = new HashMap ( );
                    map.put ( "categoryId" ,baseCategoryView.getCategory3Id ( ) );
                    map.put ( "categoryName" ,baseCategoryView.getCategory3Name ( ) );
                    return map;

                } ).collect ( Collectors.toList ( ) );
                category2IdMap.put ( "categoryChild", mapList);
                category2IdMapList.add ( category2IdMap );
            }
            category1IdMap.put ( "categoryChild",category2IdMapList );
            System.out.println("一级分类：次数：" + index);
            resultListMap.add ( category1IdMap );

        }
        return resultListMap;
    }
    @GetMapping("/")
    public String index(){

        return "index";
    }
//    @GetMapping("/hello")
//    public String hello(Model model){
//        model.addAttribute ("world","你好世界");
//        return "hello";
//    }
}
